﻿Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Xml
Imports Mysoft.Map.Data
Imports System.Web

Imports System.Web.UI.Design
Imports System.Text

<ToolboxData("<{0}:TabsAndNavs runat=server></{0}:TabsAndNavs>")> Public Class TabsAndNavs
    Inherits System.Web.UI.Control
   
    Private _blShowTitle As Boolean
    Private _strType As String

#Region "定义属性"
    '自定义过滤条件
    <Category("Data"), Description("自定义Xml文件路径"), DefaultValue("")> Property [Xml]() As String
        Get
            If (ViewState("Xml") Is Nothing) Then
                Try
                    If (Not Context.Request.QueryString("Xml") Is Nothing) Then
                        ViewState("Xml") = Context.Request.QueryString("Xml")
                    Else
                        ViewState("Xml") = Context.Request.Path.Replace(".aspx", ".xml")
                    End If
                Catch exception1 As Exception
                    ViewState("Xml") = ""
                End Try
            End If

            Return Convert.ToString(ViewState("Xml"))
        End Get

        Set(ByVal value As String)
            ViewState("Xml") = value
        End Set
    End Property

            '自定义过滤条件
    <Category("Data"), Description("自定义数据过滤条件，替换 SQL 语句中的 ""2=2""。"), DefaultValue("")> Property tabCustomFilter() As String
        Get
            If ViewState("tabCustomFilter") Is Nothing Then
                Return "(2=2)"
            End If
            Return Convert.ToString(ViewState("tabCustomFilter"))
        End Get
        Set(ByVal Value As String)
            ViewState("tabCustomFilter") = Value
        End Set
    End Property

    '自定义过滤条件
    <Category("Data"), Description("自定义数据过滤条件，替换 SQL 语句中的 ""2=2""。"), DefaultValue("")> Property navCustomFilter() As String
        Get
            If ViewState("navCustomFilter") Is Nothing Then
                Return "(2=2)"
            End If
            Return Convert.ToString(ViewState("navCustomFilter"))
        End Get
        Set(ByVal Value As String)
            If Value Is Nothing Then
                Value = "(2=2)"
            End If
            ViewState("navCustomFilter") = Value
        End Set
    End Property

    <Category("Behavior"), Description("前端事件 onBeforeClick 触发时调用的前端函数名。"), DefaultValue("")> Overridable Property onBeforeClick() As String
        Get
            If Not ViewState("onBeforeClick") Is Nothing Then
                Return CStr(ViewState("onBeforeClick"))
            End If
            Return String.Empty
        End Get
        Set(ByVal Value As String)
            ViewState("onBeforeClick") = Value
        End Set
    End Property

    <Category("Behavior"), Description("前端事件 onAfterClick 触发时调用的前端函数名。"), DefaultValue("")> Overridable Property onAfterClick() As String
        Get
            If Not ViewState("onAfterClick") Is Nothing Then
                Return CStr(ViewState("onAfterClick"))
            End If
            Return String.Empty
        End Get
        Set(ByVal Value As String)
            ViewState("onAfterClick") = Value
        End Set
    End Property
#End Region

#Region "控件输出"

    Protected Sub RenderTabs(ByVal writer As System.Web.UI.HtmlTextWriter, ByVal xnlT As XmlNodeList)
        Dim i, iCnt, iIndex As Integer
        Dim jRowIndex, jRowCnt, jColIndex, jColCnt As Integer
        Dim dtTmp As DataTable
        Dim xnTmp As XmlNode
        Dim xnParamTmp As XmlNode

        iIndex = 0

        If _strType = "0" OrElse _strType = "2" Then
            writer.Write("<td id=""" & Me.ID & "_tdTabs"" style=""text-align:left;""  class=""Header"">")
            writer.Write("<div id=""" & Me.ID & "_divTabs"" style=""overflow: hidden; width: 100%; height: 100%;"">")
            writer.Write("<table cellspacing=""0"" cellpadding=""0""  border=""0"">")
            writer.Write("<tr>")
            writer.Write("<td id=""" & Me.ID & "_Tabs"" nowrap valign=""bottom"" style=""position: relative; z-index: 2;"">")

            iCnt = xnlT.Count - 1

            For i = 0 To iCnt
                xnTmp = xnlT.Item(i)

                xnParamTmp = xnTmp.SelectSingleNode("params")

                If Not xnTmp.Attributes("sql") Is Nothing AndAlso xnTmp.Attributes("sql").Value <> "" Then
                    dtTmp = MyDB.GetDataTable(Mysoft.Map.Utility.General.ReplaceKeywords(xnTmp.Attributes("sql").Value.Replace("2=2", Me.tabCustomFilter)))

                    If Not dtTmp Is Nothing AndAlso dtTmp.Rows.Count > 0 Then
                        jColCnt = dtTmp.Columns.Count - 1
                        jRowCnt = dtTmp.Rows.Count - 1

                        For jRowIndex = 0 To jRowCnt
                            writer.Write("<span tabindex=""" & iIndex.ToString & """ class=""tab"" ")

                            For jColIndex = 0 To jColCnt
                                writer.Write(dtTmp.Columns(jColIndex).ColumnName & "=""" & dtTmp.Rows(jRowIndex).Item(jColIndex).ToString & """ ")
                            Next

                            If Not xnParamTmp Is Nothing Then
                                writer.Write("params='" & System.Web.HttpUtility.HtmlEncode(xnParamTmp.OuterXml.Replace("\r", "").Replace("\n", "")) & "' ")
                            End If

                            For Each xa As XmlAttribute In xnTmp.Attributes
                                If xa.Name <> "sql" Then
                                    writer.Write(xa.Name & "=""" & System.Web.HttpUtility.HtmlEncode(xa.Value) & """ ")
                                End If
                            Next

                            Try
                                writer.Write(">" & System.Web.HttpUtility.HtmlEncode(dtTmp.Rows(jRowIndex).Item("title").ToString) & "</span>")
                            Catch ex As Exception
                                writer.Write(">" & System.Web.HttpUtility.HtmlEncode(xnTmp.Attributes("title").Value) & "</span>")
                            End Try

                            iIndex = iIndex + 1
                        Next
                    End If
                Else
                    writer.Write("<span tabindex=""" & iIndex.ToString & """ class=""tab"" ")

                    If Not xnParamTmp Is Nothing Then
                        writer.Write("params='" & System.Web.HttpUtility.HtmlEncode(xnParamTmp.OuterXml.Replace("\r", "").Replace("\n", "")) & "' ")
                    End If

                    For Each xa As XmlAttribute In xnTmp.Attributes
                        writer.Write(xa.Name & "=""" & System.Web.HttpUtility.HtmlEncode(xa.Value) & """ ")
                    Next

                    writer.Write(">" & System.Web.HttpUtility.HtmlEncode(xnTmp.Attributes("title").Value) & "</span>")
                    iIndex = iIndex + 1
                End If
            Next

            writer.Write("</td>")
            writer.Write("</tr>")
            writer.Write("</table>")
            writer.Write("</div>")
            writer.Write("</td>")
            writer.Write("<td id=""" & Me.ID & "_tdMoveTab"" style=""width: 30px;padding-top:3px;display:none""  class=""Header""> ")
            writer.Write("<div style=""width: 28px;"">")
            writer.Write("<img src=""/gljsc/image/tabnavleft.png"" title=""向左滚动标签"" style=""cursor: hand;display:none"" onclick=""" & Me.ID & ".moveright()"" />&nbsp;")
            writer.Write("<img src=""/gljsc/image/tabnavright.png"" title=""向右滚动标签"" style=""cursor: hand;display:none"" onclick=""" & Me.ID & ".moveleft()"" /></div>")
            writer.Write("</td>")
            writer.Write("</tr>")
        Else
            If _blShowTitle Then
                writer.Write("</tr>")
            End If
        End If
    End Sub

    Protected Sub RenderNavs(ByVal writer As System.Web.UI.HtmlTextWriter, ByVal xnlN As XmlNodeList)
        Dim i, iCnt, iIndex As Integer
        Dim jRowIndex, jRowCnt, jColIndex, jColCnt As Integer
        Dim dtTmp As DataTable
        Dim xnTmp As XmlNode
        Dim xnParamTmp As XmlNode
        Dim strShow As String
        iIndex = 0

        If _strType = "1" OrElse _strType = "2" Then
            writer.Write("<tr>")

            If _strType = "2" Then
                writer.Write("<td colspan=""3"" valign=""top"">")
            Else
                writer.Write("<td valign=""top"">")
            End If

            If _strType = "2" Then
                writer.Write("<hr id=""" & Me.ID & "_hrGlowTab"" class=""tabGlow"">")
            End If

            writer.Write("<table id=""" & Me.ID & "_tbContent"" cellspacing=""0"" cellpadding=""0"" style=""display:none;height:100%;width:100%;"" border= ""0"">")
            writer.Write("<tr valign=""top"">")
            writer.Write("<td class=""leftBar"" valign=""top"">")
            writer.Write("<div id=""" & Me.ID & "_divNavs"" style=""overflow: hidden;"">")
            writer.Write("<span id=""" & Me.ID & "_Navs"" style=""position: relative; z-index: 2;"">")

            iCnt = xnlN.Count - 1

            For i = 0 To iCnt
                xnTmp = xnlN.Item(i)

                xnParamTmp = xnTmp.SelectSingleNode("params")

                If Not xnTmp.Attributes("sql") Is Nothing AndAlso xnTmp.Attributes("sql").Value <> "" Then
                    dtTmp = MyDB.GetDataTable(Mysoft.Map.Utility.General.ReplaceKeywords(xnTmp.Attributes("sql").Value.Replace("2=2", Me.navCustomFilter)))

                    If Not dtTmp Is Nothing AndAlso dtTmp.Rows.Count > 0 Then
                        jColCnt = dtTmp.Columns.Count - 1
                        jRowCnt = dtTmp.Rows.Count - 1

                        For jRowIndex = 0 To jRowCnt
                            writer.Write("<div class=""lbItem"" tabindex=""" & iIndex.ToString & """ ")

                            For jColIndex = 0 To jColCnt
                                If dtTmp.Columns(jColIndex).ColumnName <> "icon" Then
                                    writer.Write(dtTmp.Columns(jColIndex).ColumnName & " =""" & dtTmp.Rows(jRowIndex).Item(jColIndex).ToString & """ ")
                                End If
                            Next

                            If Not xnParamTmp Is Nothing Then
                                writer.Write("params='" & System.Web.HttpUtility.HtmlEncode(xnParamTmp.OuterXml.Replace("\r", "").Replace("\n", "")) & "' ")
                            End If

                            For Each xa As XmlAttribute In xnTmp.Attributes
                                If xa.Name <> "icon" AndAlso xa.Name <> "sql" Then
                                    writer.Write(xa.Name & "=""" & System.Web.HttpUtility.HtmlEncode(xa.Value) & """ ")
                                End If
                            Next

                            strShow = ""

                            Try
                                strShow = "><img src=""" & System.Web.HttpUtility.HtmlEncode(dtTmp.Rows(jRowIndex).Item("icon").ToString) & """ align=""absmiddle"">"
                            Catch ex As Exception

                            End Try

                            Try
                                If strShow = "" Then
                                    strShow = ">" & System.Web.HttpUtility.HtmlEncode(GetStrForZdCd(dtTmp.Rows(jRowIndex).Item("title").ToString, 12)) & "</div>"
                                Else
                                    strShow = strShow & System.Web.HttpUtility.HtmlEncode(GetStrForZdCd(dtTmp.Rows(jRowIndex).Item("title").ToString, 12)) & "</div>"
                                End If
                            Catch ex As Exception

                            End Try

                            If strShow = "" Then
                                If xnTmp.Attributes("icon") Is Nothing Then
                                    writer.Write(">" & System.Web.HttpUtility.HtmlEncode(GetStrForZdCd(xnTmp.Attributes("title").Value, 12)) & "</div>")
                                Else
                                    writer.Write("><img src=""" & System.Web.HttpUtility.HtmlEncode(xnTmp.Attributes("icon").Value) & """ align=""absmiddle"">" & System.Web.HttpUtility.HtmlEncode(GetStrForZdCd(xnTmp.Attributes("title").Value, 12)) & "</div>")
                                End If
                            Else
                                If Right(strShow, 6) = "</div>" Then
                                    writer.Write(strShow)
                                Else
                                    writer.Write(strShow & System.Web.HttpUtility.HtmlEncode(GetStrForZdCd(xnTmp.Attributes("title").Value, 12)) & "</div>")
                                End If
                            End If

                            iIndex = iIndex + 1
                        Next
                    End If
                Else
                    writer.Write("<div class=""lbItem"" tabindex=""" & iIndex.ToString & """ ")

                    If Not xnParamTmp Is Nothing Then
                        writer.Write("params='" & System.Web.HttpUtility.HtmlEncode(xnParamTmp.OuterXml.Replace("\r", "").Replace("\n", "")) & "' ")
                    End If

                    For Each xa As XmlAttribute In xnTmp.Attributes
                        If xa.Name <> "icon" AndAlso xa.Name <> "sql" Then
                            writer.Write(xa.Name & "=""" & System.Web.HttpUtility.HtmlEncode(xa.Value) & """ ")
                        End If
                    Next

                    If xnTmp.Attributes("icon") Is Nothing Then
                        writer.Write(">" & System.Web.HttpUtility.HtmlEncode(GetStrForZdCd(xnTmp.Attributes("title").Value, 12)) & "</div>")
                    Else
                        writer.Write("><img src=""" & System.Web.HttpUtility.HtmlEncode(xnTmp.Attributes("icon").Value) & """ align=""absmiddle"">" & System.Web.HttpUtility.HtmlEncode(GetStrForZdCd(xnTmp.Attributes("title").Value, 12)) & "</div>")
                    End If

                    iIndex = iIndex + 1
                End If
            Next

            writer.Write("</span>")
            writer.Write("</div>")
            writer.Write("<hr id=""" & Me.ID & "_hrGlowNav"" class=""navGlow"">")
            writer.Write("</td>")
            writer.Write("<td id=""" & Me.ID & "_tdTabNavContent"" valign=""top"">")
            writer.Write("</td>")
            writer.Write("</tr>")
            writer.Write("<tr id=""" & Me.ID & "_trMoveNavs"" height=""15px"" style=""display: none"">")
            writer.Write("<td align=""right"" NOWRAP>")
            writer.Write("<table cellspacing=""0"" cellpadding=""0"" style=""height: 100%; width: 81px;"" border=""0"">")
            writer.Write("<tr valign=""top"">")
            writer.Write("<td valign=""top"" class=""GDTBar"">")
            writer.Write("<div style=""width: 15px; vertical-align: bottom; text-align: center;display:none"" NOWRAP>&nbsp;")
            writer.Write("<img src=""/gljsc/image/tabnavup.png"" title=""向上滚动标签"" style=""cursor: hand;"" onclick=""" & Me.ID & ".movedown()"" />&nbsp;</div><div style=""width: 15px; vertical-align: bottom; text-align: center;display:none"" NOWRAP>&nbsp;")
            writer.Write("<img src=""/gljsc/image/tabnavdown.png"" title=""向下滚动标签"" style=""cursor: hand;"" onclick=""" & Me.ID & ".moveup()"" />&nbsp;</div>")
            writer.Write("</td>")
            writer.Write("</tr>")
            writer.Write("</table>")
            writer.Write("</td>")
            writer.Write("<td>")
            writer.Write("</td>")
            writer.Write("</tr>")
            writer.Write("</table>")
            writer.Write("</td>")
            writer.Write("</tr>")
            writer.Write("</table>")
        Else
            writer.Write("<tr>")

            If _strType = "0" Then
                writer.Write("<td colspan=""3"" valign=""top"">")
            Else
                writer.Write("<td valign=""top"">")
            End If

            writer.Write("<hr id=""" & Me.ID & "_hrGlowTab"" class=""tabGlow"">")
            writer.Write("<table id=""" & Me.ID & "_tbContent"" cellspacing=""0"" cellpadding=""0"" style=""display:none"" border= ""0"">")
            writer.Write("<tr valign=""top"">")
            writer.Write("<td id=""" & Me.ID & "_tdTabNavContent"" valign=""top"">")
            writer.Write("</td>")
            writer.Write("</tr>")
            writer.Write("</table>")
            writer.Write("</td>")
            writer.Write("</tr>")
            writer.Write("</table>")
        End If
    End Sub

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        Dim strDefaultArea As String = ""
        Dim strTitle As String
        Dim iTitleWidth As String
        Dim xmldoc As New XmlDocument
        Try
            xmldoc.Load(Me.Context.Server.MapPath(Me.xml))
        Catch ex As Exception
            writer.Write("加载 XML 错误。")
            Exit Sub
        End Try
        Dim xn As XmlNode
        Dim xnlT As XmlNodeList
        Dim xnlN As XmlNodeList
        xn = xmldoc.SelectSingleNode("/page/controls/control[@id=""" & Me.ID & """]")
        If xn Is Nothing Then
            writer.Write("加载 XML 错误。")
            Exit Sub
        Else
            xnlT = xn.SelectNodes("tabs/tab")
            xnlN = xn.SelectNodes("navs/nav")
            If xnlT.Count = 0 Then
                If xnlN.Count > 0 Then
                    _strType = "1"
                End If
            Else
                If xnlN.Count = 0 Then
                    _strType = "0"
                Else
                    _strType = "2"
                End If
            End If

            If Not xn.Attributes("showTitle") Is Nothing AndAlso CType(xn.Attributes("showTitle").Value, Boolean) Then
                If xn.Attributes("title") Is Nothing Then
                    writer.Write("加载 XML 错误。")
                    Exit Sub
                End If
                _blShowTitle = True
                StrTitle = xn.Attributes("title").Value
            End If

            If Not xn.Attributes("titleWidth") Is Nothing Then
                iTitleWidth = xn.Attributes("titleWidth").Value
            End If

            If Not xn.Attributes("defaultArea") Is Nothing Then
                strDefaultArea = xn.Attributes("defaultArea").Value
            End If

            writer.Write("<table width=""100%"" height=""100%"" id=""" & Me.ID & """ border=""0"" cellspacing=""0"" cellpadding=""0"" class=""tabBarNav""" & IIf(strDefaultArea = "", "", " defaultArea=""" & strDefaultArea & """") & " ctltype=""" & _strType & """")

            If Me.onBeforeClick <> "" Then writer.Write(" onbeforeclick=""" & System.Web.HttpUtility.HtmlEncode(Me.onBeforeClick) & """")
            If Me.onAfterClick <> "" Then writer.Write(" onafterclick=""" & System.Web.HttpUtility.HtmlEncode(Me.onAfterClick) & """")

            For Each xa As XmlAttribute In xn.Attributes
                If xa.Name <> "showTitle" AndAlso xa.Name <> "title" AndAlso xa.Name <> "titleWidth" AndAlso xa.Name <> "defaultArea" AndAlso xa.Name <> "id" Then
                    writer.Write(xa.Name & "=""" & System.Web.HttpUtility.HtmlEncode(xa.Value) & """ ")
                End If
            Next
            Dim xnParamTmp As XmlNode = xn.SelectSingleNode("params")

            If Not xnParamTmp Is Nothing Then
                writer.Write("params='" & System.Web.HttpUtility.HtmlEncode(xnParamTmp.OuterXml.Replace("\r", "").Replace("\n", "")) & "' ")
            End If

            writer.Write(">")

            If _blShowTitle Then
                If _strType <> "1" Then
                    writer.Write("<tr height=""24px""><td style=""width:" & IIf(iTitleWidth = "", "80", iTitleWidth) & "px;border-bottom:1px solid #6f9dd9;text-align:left;padding-left:5px;padding-top: 4px;"" class=""Header"">" & IIf(strTitle = "", "&nbsp;", strTitle) & "</td>")
                Else
                    writer.Write("<tr height=""24px""><td style=""width:100%;border-bottom:1px solid #6f9dd9;text-align:left;padding-left:5px;padding-top: 4px;"" class=""Header"">" & IIf(strTitle = "", "&nbsp;", strTitle) & "</td>")
                End If
            Else
                If _strType = "0" OrElse _strType = "2" Then
                    writer.Write("<tr height=""24px""><td style=""width:" & IIf(iTitleWidth = "", "10", iTitleWidth) & "px;border-bottom:1px solid #6f9dd9""  class=""Header"">&nbsp;</td>")
                End If
            End If

            RenderTabs(writer, xnlT)

            RenderNavs(writer, xnlN)
        End If
    End Sub

    Private Function GetStrForZdCd(ByVal strTitle As String, ByVal maxLength As Integer) As String
        Dim strRet As String
        Dim bb As Byte()

        '//把BB转换成字节数组

        bb = System.Text.Encoding.Default.GetBytes(strTitle.ToCharArray())
        If bb.Length <= maxLength Then
            Return strTitle
        End If

        '//截取×××位
        strRet = New String(System.Text.Encoding.Default.GetChars(bb, 0, maxLength))

        Dim i As Integer = 0

        i = strTitle.IndexOf(strRet)
        If i = -1 Then
            strRet = New String(System.Text.Encoding.Default.GetChars(bb, 0, maxLength - 1))
        End If
        Return strRet
    End Function
#End Region
End Class
